<?php
namespace app\agent\controller;
use think\Controller;
use think\Db;
use app\common\controller\AgentBaseController;
use app\shop\model\MemberModel;

class Accounts extends AgentBaseController
{
	//解除合伙人	
	public function jiechu(){
		$res=db('info_user')->where("id",input("id"))->update(["is_pt"=>0,'tj_pt'=>0]);
		if($res){
			return json(['code'=>404,'msg'=>'操作成功']);
		}else{
			return json(['code'=>404,'msg'=>'操作失败']);
		}
	}	
	//添加合伙人
	public function pt_add(){
		if($_POST){
			$agent_id = session('agent_id');
			$where['tj_agent']=$agent_id;
			$where['id']=input("mobile");
			$user=db('info_user')->where($where)->find();
			if(!$user){
				return json(['code'=>404,'msg'=>'会员编号不存在!无法添加']);
			}else{
				$res=db('info_user')->where($where)->update(["is_pt"=>1,'tj_pt'=>$user['id'],'pt_time'=>now_time()]);
				if($res){
					return json(['code'=>200,'msg'=>'操作成功']);
				}else{
					return json(['code'=>404,'msg'=>'操作失败']);
				}
			}
		}
		return $this->fetch();
	}		
	public function pt_user(){
		$agent_id = session('agent_id');
		$where['a.tj_agent']=$agent_id;
		$where['a.is_pt']=1;
		$field="a.*,b.b,c.c";
		$list_tj=db('info_user')->field("id,level,count(id) b")->group("level")->buildSql();
		$list_tj2=db('info_user')->field("id,tj_pt,count(id) c")->group("tj_pt")->buildSql();
		$list=db('info_user ')->alias('a')->
		join($list_tj." b","b.level=a.id",'left')->
		join($list_tj2." c",'c.tj_pt=a.id','left')->
		where($where)->field($field)->paginate(20);		
		$this->assign('list',$list);
		return $this->fetch();
	}
	
    //推广会员
	public function consumer_user()
    {
    	$agentid = session('agent_id');
    	$agent_to_user = session('agent_user_id');
		$pingci=0;
		$nengli=0;
		$param=input('post.');
		if(!empty($param['mobile']))
		{
			$where['a.mobile'] = ['like','%'.$param['mobile'].'%'];
		}
		
		
		if(!empty($param['begin_time']) && !empty($param['end_time'])){
			$where['a.add_time'] = ['between time',[$param['begin_time'],$param['end_time']]];
		}
		if(input('level_id')){
			$where['a.level']=input("level_id");
		}
		$where['a.tj_agent'] = $agentid;
		$order="a.add_time";
		if(input("order")){
			if(input("order")=="tjs"){
				$order="f.".input("order");
			}else{
				$order="a.".input("order");
			}
		}
		$sql=db('info_user')->field("count(id) as tjs,level")->group("level")->buildSql();
		$sqlp=db('log_paylog_copy')->field("sum(balance) as czs,uid,sum(agent_score) as scs")->where("order_type=8")->group("uid")->buildSql();
    	$list = db('info_user')
			->alias('a')		
			->join('info_user c','c.id = a.level','LEFT')
			->join('info_agent d','d.id = a.tj_agent','LEFT')
			->join($sql.' f','f.level = a.id','LEFT')
			->join($sqlp.' p','p.uid = a.id','LEFT')
			->field('p.czs,p.scs,f.tjs,a.balance,a.score,a.id,a.nickname,a.mobile,a.is_verify,a.sex,a.add_time,c.mobile as smobile,d.agent_name')
			->where($where)
			->order("$order desc")
			->paginate(15);
    	$this->assign('list',$list);
		$this->assign('empty','<span class="empty">没有数据</span>');		
    	return $this->fetch();
    }

	//推广会员导出
    public function consumer_dc(){
		$param=input();
		$agentid = session('agent_id');
		$agent_to_user = session('agent_user_id');
		if(!empty($param['begin_time']) && !empty($param['end_time'])){
			$where['a.add_time'] = ['between time',[$param['begin_time'],$param['end_time']]];
		}
		//if(!empty($param['mobile']))$where['a.mobile'] = $param['mobile'];
				
		$where['a.tj_agent'] = $agentid;
		$charge	= db('log_user_charge')->field('sum(balance) as total_price,count(id) as total_num,u_id')->group('u_id')->buildSql();
    	$list = db('info_user')
			->alias('a')		
			->join('info_user c','c.id = a.level','LEFT')
			->join('info_agent d','d.id = a.tj_agent','LEFT')
			->field('a.id,a.nickname,a.mobile,a.is_verify,a.sex,a.add_time,c.mobile as smobile,d.agent_name')
			->where($where)
			->where('a.mobile','<>','')
			->order('a.add_time desc')
			->select();

		$str ="ID,会员名,手机号,性别,上级会员,注册时间"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$id = iconv('utf-8','gbk',$row['id']);
			$nickname = iconv('utf-8','gbk',$row['nickname'].'	');  
			$mobile = iconv('utf-8','gbk',$row['mobile'].'	');
			$sexname = iconv('utf-8','gbk',$row['sex']); 
			$up_user = iconv('utf-8','gbk',$row['smobile']);
			$add_time = iconv('utf-8','gbk','	'.$row['add_time']);
			$str .= $id.",".$nickname.",".$mobile.",".$sexname.",".$up_user.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='推广会员列表.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}

    //扫码会员
	public function scancode_user()
    {
    	$agentid = session('agent_id');
    	$agent_to_user = session('agent_user_id');
		
		$pingci=0;
		$nengli=0;
		if(request()->isPost()){
    		
    		$param=input('post.');
		
			if(input("pingci")){//消费频次
				$pingci=input("pingci");
				$pc=db('user_group')->where('id',$pingci)->find();
				$where['b.total_num']=['BETWEEN',[$pc['nums'],$pc['nums2']]];
			}
			
			if(input("nengli")){//消费能力
				$nengli=input("nengli");
				$pc=db('user_group')->where('id',$nengli)->find();
				$where['b.total_money']=['BETWEEN',[$pc['money'],$pc['money2']]];
			}
			if(!empty($param['begin_time']) && !empty($param['end_time'])){
				$where['a.add_time'] = ['BETWEEN time',[$param['begin_time'],$param['end_time']]];
			}
    	}
		$this->assign('pingci',$pingci);
		$this->assign('nengli',$nengli);
		
    	$list1 = db('user_group')->where('type',1)->select();
        $this->assign('list1',$list1);
        $list2 = db('user_group')->where('type',2)->select();
        $this->assign('list2',$list2);
		
		$where['b.agent_id'] = $agentid;

		$list = db('info_user')
				->alias('a')
				->join('user_relevance_agent b','a.id = b.user_id')
    			->where($where)
    			->order('a.add_time desc')
    			->paginate(15);
    	$this->assign('list',$list);
		$this->assign('empty','<span class="empty">没有数据</span>');
		
    	return $this->fetch();
    }

	//扫码会员导出
    public function scancode_dc(){
		$param=input();
		$agent_to_user = session('agent_user_id');
		if(!empty($param['begin_time']) && !empty($param['end_time'])){
			$where['a.add_time'] = ['between time',[$param['begin_time'],$param['end_time']]];
		}

		$where['b.agent_id'] = session('agent_id');
		$list = db('info_user')
				->alias('a')
				->join('user_relevance_agent b','a.id = b.user_id')
				->field('a.id,a.user_name,a.mobile,a.sex,a.nickname,b.total_num,b.total_money,a.add_time')
    			->where($where)
    			->order('a.add_time desc')
    			->select();
		
		$str ="ID,会员名,手机号,消费次数,消费金额,性别,注册时间"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$id = iconv('utf-8','gbk',$row['id']);
			$nickname = iconv('utf-8','gbk',$row['nickname'] ? $row['nickname']:'支付会员');  
			$mobile = iconv('utf-8','gbk',$row['mobile'].'	');
			$total_num = iconv('utf-8','gbk',$row['total_num']);
			$total_money = iconv('utf-8','gbk',$row['total_money']);
			$sexname = iconv('utf-8','gbk',$row['sex']); 
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$str .= $id.",".$nickname.",".$mobile.",".$total_num.",".$total_money.",".$sexname.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='扫码会员列表.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}


    //会员等级
    public function user_level()
    {
    	$agentid = session('agent_id');
    	$list = Db::table('user_level')->where('agent_id',$agentid)->order('add_time desc')->select();
        $this->assign('list',$list);
    	return $this->fetch();
    }
    public function user_level_add()
    {
    	$agentid = session('agent_id');
    	$oemid = session('oem_id');
    	if($_POST){
            $data['level_name']=$_POST['level_name'];
            $data['discount']=$_POST['discount'];
            $data['agent_id']=$agentid;
            $data['oem_id']=$oemid;
            $data['add_time']=now_time();
            $result=Db::table('user_level')->insert($data);
            if($result){
				return json(['code'=>200,'msg'=>'成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'失败']);
	        }
        }
    	
    	return $this->fetch();
    }
    public function user_level_update()
    {
    	$agentid = session('agent_id');
    	if($_POST){
           		$data['level_name']=input("level_name");
           		$data['discount']=input("discount");
           	$result=Db::table('user_level')->where('agent_id',$agentid)->update($data);
            if($result){
				return json(['code'=>200,'msg'=>'成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'失败']);
	        }
        }
        $list = Db::table('user_level')->where('id',$id)->find();
        $this->assign('list',$list);
    	return $this->fetch();
    }
    public function user_level_del()
    {
    	$id = input("id");
    	$result = Db::table('user_level')->where('id',$id)->delete();
        if($result){
			return json(['code'=>200,'msg'=>'成功']);
        }else{
       		return json(['code'=>402,'msg'=>'失败']);
        }
    	
    }
    //会员分组
	public function user_group()
    {
    	$agentid = session('agent_id');
    	$list = Db::table('user_group')->where('type',1)->select();
        $this->assign('list',$list);
        $datd = Db::table('user_group')->where('type',2)->select();
        $this->assign('list1',$datd);
    	return $this->fetch();
    }
    public function fenzu_setting()
    {
    	$agentid = session('agent_id');
    	$id = input('id');
    	if($_POST){
    		if($id){
            	$result=Db::table('user_group')->where('id',$id)->update($data);
            	
    		}else{
            	$result=Db::table('user_group')->insert($data);
    		}
            if($result){
				return json(['code'=>200,'msg'=>'成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'失败']);
	        }
        }
    	return $this->fetch();
    }
    public function fenzu_setting2()
    {
    	$agentid = session('agent_id');
    	$id = input('id');
    	if($_POST){
    		    $data['group_type']=2;
           		$data['number1']=input("number1");
           		$data['number2']=input("number2");
           		$data['number3']=input("number3");
           		$data['number4']=input("number4");
           		$data['number5']=input("number5");
           		$data['agent_id']=session("agent_id");
           		$data['oem_id']=session("oem_id");
    		if($id){
            	$result=Db::table('user_group')->where('id',$id)->update($data);
            	
    		}else{
            	$result=Db::table('user_group')->insert($data);
    		}
            if($result){
				return json(['code'=>200,'msg'=>'成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'失败']);
	        }
        }
        $list = Db::table('user_group')->where('agent_id',$agentid)->where('group_type',2)->find();
        $this->assign('list',$list);
    	return $this->fetch();
    }
    
	//添加线下会员
	function member_add(){
		
		$agent_info = db('info_agent')->where('id',session('agent_id'))->find();
		
		
		if(request()->isPost()){
			$param=input('post.');
			
			
			$param['add_time'] 		= now_time();
			$param['oem_id'] 		= $agent_info['oem_id'];
			$param['agency_id1'] 	= $agent_info['agency_id1'];
			$param['agency_id2'] 	= $agent_info['agency_id2'];
			$param['agent_id'] 		= $agent_info['id'];
			
			$member = new MemberModel();
			$res = $member->allowField(true)->save($param);
			
			if($res){
				return json(['code'=>200,'msg'=>'添加成功','data'=>$param]);
			}else{
				return json(['code'=>402,'msg'=>'添加失败']);
			}
		}
		
		//获取会员等级
		$user_level = db('user_level')->order('discount asc')->select();
		$this->assign('user_level',$user_level);
		
		return $this->fetch();
	}
    
    
    
}
